package de.uni.muenster.idw2.threads;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class ImageLoaderThradBuildingDialog extends AsyncTask<Void, Void, Drawable>{

	private final String path;
	private final Activity act;
	private final ImageView imageView;
	private final ProgressBar progressBar;

	public ImageLoaderThradBuildingDialog(String path, Activity act, ImageView imageView, ProgressBar progressBar){
		this.path = path;
		this.act = act;
		this.imageView = imageView;
		this.progressBar = progressBar;
		
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressBar.setVisibility(View.VISIBLE);
	}
	
	@Override
	protected Drawable doInBackground(Void... params) {
		
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			Log.e("wichtig", e.toString());
		}
		
		Drawable img = null;
		try {
			 img = Drawable.createFromStream((InputStream) new URL(path).getContent(), "draw");
			 
		} catch (OutOfMemoryError e) {
			Log.e("wichtig", e.toString());
		} catch (MalformedURLException e) {
			Log.e("wichtig", e.toString());
		} catch (IOException e) {
			Log.e("wichtig", e.toString());
		}
		return img;
	}
	
	@Override
	protected void onPostExecute(Drawable result) {
		super.onPostExecute(result);
		
		imageView.setImageDrawable(result);
		progressBar.setVisibility(View.GONE);
	}
	
	@Override
	protected void onCancelled() {
		super.onCancelled();
	}

}
